package com.taysear.parcel.fieldfactory;

import com.taysear.parcel.ParcelApplication;
import com.taysear.parcel.domain.EmployeeStatus;
import com.taysear.parcel.domain.EmployeeType;
import com.taysear.parcel.domain.Gender;
import com.taysear.parcel.domain.Station;
import com.taysear.parcel.domain.StationStatus;
import com.taysear.parcel.domain.Substation;
import com.taysear.parcel.util.Constants;
import com.vaadin.addon.jpacontainer.JPAContainer;
import com.vaadin.addon.jpacontainer.JPAContainerFactory;
import com.vaadin.addon.jpacontainer.fieldfactory.FieldFactory;
import com.vaadin.data.Item;
import com.vaadin.data.validator.StringLengthValidator;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.DateField;
import com.vaadin.ui.Field;
import com.vaadin.ui.NativeSelect;
import com.vaadin.ui.TextField;

public class EmployeeFieldFactory extends FieldFactory {
	
	ParcelApplication app;
	public final TextField employeeId = new TextField("Employee id");
	final TextField firstname = new TextField("Firstname");
	final TextField lastname = new TextField("Lastname");
	final TextField idNumber = new TextField("Id number");
	final TextField drivingLicenceNumber = new TextField("Driving licence number");
	final DateField drivingLicenceExpireDate = new DateField("Driving licence expire date");
	final TextField bloodgroup = new TextField("Bloodgroup");
	final TextField address = new TextField("Address");
	final TextField postcode = new TextField("Postcode");
	final TextField city = new TextField("City");
	final TextField tel1 = new TextField("Tel 1");
	final TextField tel2 = new TextField("Tel 2");
	NativeSelect employeeType = new NativeSelect("Type");
	final DateField joinDate = new DateField("Date joined");
	final DateField dob = new DateField("Dob");
	NativeSelect employeeStatus = new NativeSelect("Status");
	final DateField terminatedDate = new DateField("Date end of employment");
	NativeSelect gender = new NativeSelect("Gender");
	NativeSelect station = new NativeSelect("Station");
	NativeSelect substation = new NativeSelect("Substation");

	public EmployeeFieldFactory(ParcelApplication app) {
		this.app = app;
	}

	@Override
	public Field createField(Item item, Object propertyId, Component uiContext) {
		Field f = null;
		if ("employeeId".equals(propertyId)) {
			employeeId.setRequired(true);
			employeeId.setWidth(Constants.COMMON_FIELD_WIDTH);
			return employeeId;
		} if ("firstname".equals(propertyId)) {
			firstname.setRequired(true);
			firstname.setWidth(Constants.COMMON_FIELD_WIDTH);
			firstname.setRequiredError(firstname.getCaption()+"is required");
			firstname.setNullRepresentation("");
			return firstname;
		} if ("lastname".equals(propertyId)) {
			lastname.setRequired(true);
			lastname.setWidth(Constants.COMMON_FIELD_WIDTH);
			lastname.setRequiredError(lastname.getCaption()+" is required");
			lastname.setNullRepresentation("");
			return lastname;
		} if ("idNumber".equals(propertyId)) {
			idNumber.setRequired(true);
			idNumber.setWidth(Constants.COMMON_FIELD_WIDTH);
			idNumber.setRequiredError(idNumber.getCaption()+" is required");
			idNumber.setNullRepresentation("");
			return idNumber;
		} if ("drivingLicenceNumber".equals(propertyId)) {
			drivingLicenceNumber.setWidth(Constants.COMMON_FIELD_WIDTH);
			drivingLicenceNumber.setNullRepresentation("");
			return drivingLicenceNumber;
		} if ("drivingLicenceExpireDate".equals(propertyId)) {
			drivingLicenceExpireDate.setWidth(Constants.COMMON_FIELD_WIDTH);
			drivingLicenceExpireDate.setDateFormat(Constants.DATE_FORMAT_STRING);
			drivingLicenceExpireDate.setResolution(DateField.RESOLUTION_DAY);
			return drivingLicenceExpireDate;
		} if ("bloodgroup".equals(propertyId)) {
			bloodgroup.setWidth(Constants.COMMON_FIELD_WIDTH);
			bloodgroup.setNullRepresentation("");
			return bloodgroup;
		} if ("address".equals(propertyId)) {
			address.setRequired(true);
			address.setWidth(Constants.COMMON_FIELD_WIDTH);
			address.setRequiredError(address.getCaption()+" is required");
			address.setNullRepresentation("");
			return address;
		} if ("postcode".equals(propertyId)) {
			postcode.setRequired(true);
			postcode.setWidth(Constants.COMMON_FIELD_WIDTH);
			postcode.setRequiredError(postcode.getCaption()+" is required");
			postcode.setNullRepresentation("");
			return postcode;
		} if ("city".equals(propertyId)) {
			city.setRequired(true);
			city.setWidth(Constants.COMMON_FIELD_WIDTH);
			city.setRequiredError(city.getCaption()+" is required");
			city.setNullRepresentation("");
			return city;
		} if ("tel1".equals(propertyId)) {
			tel1.setRequired(true);
			tel1.setWidth(Constants.COMMON_FIELD_WIDTH);
			tel1.setRequiredError(tel1.getCaption()+" is required");
			tel1.setNullRepresentation("");
			return tel1;
		} if ("tel2".equals(propertyId)) {
			tel2.setWidth(Constants.COMMON_FIELD_WIDTH);
			tel2.setNullRepresentation("");
			return tel2;
		} if ("employeeType".equals(propertyId)) {
			try{
				employeeType = (NativeSelect) super.createField(item, propertyId, uiContext);
			} catch (ClassCastException e) {
				JPAContainer<EmployeeType> container = JPAContainerFactory.make(EmployeeType.class, app.PERSISTENCE_UNIT);
				employeeType.setContainerDataSource(container);
			}
			employeeType.setWidth(Constants.COMMON_FIELD_WIDTH);
			employeeType.setNullSelectionAllowed(false);
			employeeType.setItemCaptionMode(ComboBox.ITEM_CAPTION_MODE_ITEM);
			employeeType.setRequired(true);
			employeeType.setRequiredError(employeeType.getCaption()+" is required");
			return employeeType;
		} if ("joinDate".equals(propertyId)) {
			joinDate.setWidth(Constants.COMMON_FIELD_WIDTH);
			joinDate.setRequired(true);
			joinDate.setRequiredError(joinDate.getCaption()+" is required");
			joinDate.setDateFormat(Constants.DATE_FORMAT_STRING);
			joinDate.setResolution(DateField.RESOLUTION_DAY);
			return joinDate;
		} if ("dob".equals(propertyId)) {
			dob.setWidth(Constants.COMMON_FIELD_WIDTH);
			dob.setRequired(true);
			dob.setRequiredError(dob.getCaption()+" is required");
			dob.setDateFormat(Constants.DATE_FORMAT_STRING);
			dob.setResolution(DateField.RESOLUTION_DAY);
			return dob;
		} if ("employeeStatus".equals(propertyId)) {
			try{
				employeeStatus = (NativeSelect) super.createField(item, propertyId, uiContext);
			} catch (ClassCastException e) {
				JPAContainer<EmployeeStatus> container = JPAContainerFactory.make(EmployeeStatus.class, app.PERSISTENCE_UNIT);
				employeeStatus.setContainerDataSource(container);
			}
			employeeStatus.setWidth(Constants.COMMON_FIELD_WIDTH);
			employeeStatus.setNullSelectionAllowed(false);
			employeeStatus.setItemCaptionMode(ComboBox.ITEM_CAPTION_MODE_ITEM);
			employeeStatus.setRequired(true);
			employeeStatus.setRequiredError(employeeStatus.getCaption()+" is required");
			return employeeStatus;
		} if ("terminatedDate".equals(propertyId)) {
			terminatedDate.setWidth(Constants.COMMON_FIELD_WIDTH);
			terminatedDate.setDateFormat(Constants.DATE_FORMAT_STRING);
			terminatedDate.setResolution(DateField.RESOLUTION_DAY);
			return terminatedDate;
		} if ("gender".equals(propertyId)) {
			try{
				gender = (NativeSelect) super.createField(item, propertyId, uiContext);
			} catch (ClassCastException e) {
				JPAContainer<Gender> container = JPAContainerFactory.make(Gender.class, app.PERSISTENCE_UNIT);
				gender.setContainerDataSource(container);
			}
			gender.setNullSelectionAllowed(false);
			gender.setItemCaptionMode(ComboBox.ITEM_CAPTION_MODE_ITEM);
			gender.setRequired(true);
			gender.setWidth(Constants.COMMON_FIELD_WIDTH);
			gender.setRequiredError(gender.getCaption()+" is required");
			return gender;
		} if ("station".equals(propertyId)) {
			try{
				station = (NativeSelect) super.createField(item, propertyId, uiContext);
			} catch (ClassCastException e) {
				JPAContainer<Station> container = JPAContainerFactory.make(Station.class, app.PERSISTENCE_UNIT);
				station.setContainerDataSource(container);
			}
			station.setWidth(Constants.COMMON_FIELD_WIDTH);
			station.setNullSelectionAllowed(false);
			station.setItemCaptionMode(ComboBox.ITEM_CAPTION_MODE_ITEM);
			station.setRequired(true);
			station.setRequiredError("Station is required");
			return station;
		} if ("substation".equals(propertyId)) {
			try{
				substation = (NativeSelect) super.createField(item, propertyId, uiContext);
			} catch (ClassCastException e) {
				JPAContainer<Substation> container = JPAContainerFactory.make(Substation.class, app.PERSISTENCE_UNIT);
				substation.setContainerDataSource(container);
			}
			substation.setWidth(Constants.COMMON_FIELD_WIDTH);
			substation.setNullSelectionAllowed(false);
			substation.setItemCaptionMode(ComboBox.ITEM_CAPTION_MODE_ITEM);
			substation.setRequired(true);
			substation.setRequiredError(substation.getCaption()+" is required");
			return substation;
		} else {
			// Use the super class to create a suitable field base on the
			// property type.
			f = super.createField(item, propertyId, uiContext);
		}
		return f;
	}
}
